/* 
 * File:   SockEndPointAsincronico.h
 * Author: max
 *
 * Created on June 1, 2013, 3:06 PM
 */

#ifndef SOCKENDPOINTASINCRONICO_H
#define	SOCKENDPOINTASINCRONICO_H

#include <queue>
#include <stdint.h>
#include <string>
#include "server_SockEndPoint.h"
#include "common_Thread.h"
#include "common_Mutex.h"

using namespace std;
using namespace TP::Threading;
using namespace TP::Comunicacion;

namespace TP { namespace Comunicacion { namespace Asincronica {

class SockEndPointAsincronico : Thread {

public:

    SockEndPointAsincronico(SockEndPoint* pEndPoint);

    SockEndPointAsincronico(int &pFileDescriptor, struct sockaddr_in* &pClientAddress);
    
    SockEndPointAsincronico(int &pFileDescriptor, struct sockaddr_in* &pClientAddress, const int &pTimeout);

    virtual ~SockEndPointAsincronico();
    
    void Iniciar() throw (Excepcion &);
    
    void Finalizar () throw (Excepcion &);
    
    void Escribir (const uint8_t* pData, const size_t &pLongitudData) throw (Excepcion&);
    
    void Leer (uint8_t* &pData, uint32_t &pLongitudData) throw (Excepcion&);
    
protected:
    virtual void tarea ();

private:

    queue<string*>& bufferIn();
    
    queue<string*>& bufferOut();
    
    SockEndPoint* _endPoint;
    
    bool _transmitiendo;
    queue<string*> _bufferIn;
    queue<string*>   _bufferOut;
    Mutex _mutexBufferIn
        , _mutexBufferOut;
    
};

}}}

#endif	/* SOCKENDPOINTASINCRONICO_H */

